//
//  WZRemindController.m
//  WZEfengAndEtong
//
//  Created by wanzhao on 16/6/14.
//  Copyright © 2016年 wanzhao. All rights reserved.
//

#import "WZRemindController.h"
#import "WZRemindCell.h"
#import "WZScheduleRemindController.h"
#import "WZDetailSchedule.h"
#import "MMPickerView.h"



@interface WZRemindController ()<UIPickerViewDelegate, UIPickerViewDataSource>
@property (weak, nonatomic) IBOutlet UIImageView *statusImgView;
//@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, weak) UIPickerView *pickerView;
@property (nonatomic, weak) UIView *containerView;

@property (nonatomic, strong) NSArray *pickerArray1;
@property (nonatomic, strong) NSArray *pickerArray2;

@end

@implementation WZRemindController

static NSString * const remindCell = @"remindCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupTableView];
    
    [self addPickerView];
}

- (void)viewWillDisappear:(BOOL)animated {
    
    
}


#pragma mark -- present RemindVC
- (void)rightDoneBtnDidClick {
    
    // 修改模型
    
    if (_block) {
        
        _block(self.detailSchedule);
        
    };
    
    
    if (_addVcBlock) {
        
        _addVcBlock(self.addFlag, self.addLength, self.addUnitName);

    }
    
    //    NSString *timeStamp = [NSDate getTimeStamp];
    
//    NSString *oaUserName = [USER_DEFAULT objectForKey:@"oaUserName"];
//    NSString *oaPwd = [USER_DEFAULT objectForKey:@"oaPwd"];
//        // 编辑日程 editFlag = @"2"
//        [NetWorkShare saveScheduleWithUserName:oaUserName pwd:oaPwd scheduleID:self.detailSchedule.scheduleID scheduleTitle:self.detailSchedule.title startTime:timeStamp alertTime:self.detailSchedule.beforeLength alertFlag:self.detailSchedule.alertFlag beforeLength:self.detailSchedule.beforeLength beforeUnitFlag:self.detailSchedule.beforeUnitFlag editFlag:@"2" successHandle:^(AFHTTPRequestOperation *operation, id responseObject) {
//    
//            if (responseObject) {
//                if ([[responseObject objectForKey:@"success"] boolValue]) {
//                    [MBProgressHUD showSuccess:[responseObject objectForKey:@"message"]];
//                    WZLog(@"result = %@", responseObject);
//                } else {
//    
//                    [MBProgressHUD showError:[responseObject objectForKey:@"message"]];
//                }
//            } else {
//    
//                [MBProgressHUD showError:UNKNOWNERROR];
//            }
//    
//        } failureHandle:^(AFHTTPRequestOperation *operation, NSError *error) {
//    
//            [MBProgressHUD showError:NETWORKNERROR];
//        }];


    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)setupTableView {
    
    self.navigationItem.title = @"定时提醒";
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem BarButtonItemWithImageName:nil title:@"确定" target:self sel:@selector(rightDoneBtnDidClick) titleFont:Font17 titleColor:WhiteColor];
    self.tableView.backgroundColor = WZRGBColor(243, 243, 243);
    
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, HARDWARE_SCREEN_WIDTH, 0)];
    self.tableView.scrollEnabled = NO;
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([WZRemindCell class]) bundle:nil] forCellReuseIdentifier:remindCell];
}


#pragma mark - cell did select
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    if (0 == indexPath.section) {
                
        switch (indexPath.row) {
            case 0:
                self.detailSchedule.alertFlag = @"1"; // 不提醒
                
                // 新建日程VC
                self.addFlag = @"1";
                
                break;
                
            case 1:
                self.detailSchedule.alertFlag = @"2"; // 提醒
                self.detailSchedule.beforeLength = @"5";
                self.detailSchedule.beforeUnitName = @"分钟";
                self.detailSchedule.beforeUnitFlag = @"1";
                
                // 新建日程Vc
                self.addFlag = @"2";
                self.addLength = @"5";
                self.addUnitName = @"分钟";
                
                break;
                
            case 2:
                self.detailSchedule.alertFlag = @"2"; // 提醒
                self.detailSchedule.beforeLength = @"15";
                self.detailSchedule.beforeUnitName = @"分钟";
                self.detailSchedule.beforeUnitFlag = @"1";
                
                self.addFlag = @"2";
                self.addLength = @"5";
                self.addUnitName = @"分钟";
                break;
                
            case 3:
                self.detailSchedule.alertFlag = @"2"; //
                self.detailSchedule.beforeLength = @"30";
                self.detailSchedule.beforeUnitName = @"分钟";
                self.detailSchedule.beforeUnitFlag = @"1";
                
                self.addFlag = @"2";
                self.addLength = @"30";
                self.addUnitName = @"分钟";
                break;
                
            case 4:
                self.detailSchedule.alertFlag = @"2"; //
                self.detailSchedule.beforeLength = @"1";
                self.detailSchedule.beforeUnitName = @"小时";
                self.detailSchedule.beforeUnitFlag = @"2";
                
                self.addFlag = @"2";
                self.addLength = @"1";
                self.addUnitName = @"小时";
                break;
                
            default:
                break;
        }
    }
    
    
    if (1 == indexPath.section) {
        
        if (0 == indexPath.row) {
            
            [UIView animateWithDuration:0.25 animations:^{
                
                self.containerView.y = self.view.height - 200;
            }];
        }
    }
}

#pragma mark - <UITableViewDataSource>
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 2;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (0 == section) {
        
        return 5;
        
    } else {
        
        return 1;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    // load xib
//    WZRemindCell *cell = [tableView dequeueReusableCellWithIdentifier:remindCell];
    
    WZRemindCell *cell = [WZRemindCell remindCellWithTableView:tableView indexPath:indexPath];
    cell.detailSchedule = self.detailSchedule;
//    WZLog(@"cell height = %f", cell.height);
    return cell;
}


// 创建pickerView
- (void)addPickerView {
    
    UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.height, self.view.width, 200)];
    [self.view addSubview:containerView];
//    containerView.backgroundColor = [UIColor redColor];
    self.containerView = containerView;
    
    UIToolbar *pickerToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 44)];
    pickerToolBar.barStyle = UIBarStyleDefault;
    [pickerToolBar sizeToFit];
//    pickerToolBar.backgroundColor = [UIColor greenColor];
    [containerView addSubview:pickerToolBar];
    
    UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(cancelBtnDidClick)];
    [cancelBtn setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:14],NSFontAttributeName, nil] forState:UIControlStateNormal];
    
    [cancelBtn setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:14],NSFontAttributeName, nil] forState:UIControlStateNormal];
    
    UIBarButtonItem *toobarDoneBtn = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(tooBarDoneBtnDidClick)];
    
    [toobarDoneBtn setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:14],NSFontAttributeName, nil] forState:UIControlStateNormal];
    
    UIBarButtonItem *fixSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
//    [pickerToolBar setItems:[NSArray arrayWithObjects:cancelBtn, fixSpace, toobarDoneBtn, nil] animated:YES];
    
    pickerToolBar.items = @[cancelBtn, fixSpace, toobarDoneBtn];
    
    UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, self.view.width, 156)];
    pickerView.showsSelectionIndicator = YES;
    pickerView.delegate = self;
    pickerView.dataSource = self;
    pickerView.backgroundColor = [UIColor whiteColor];
//    pickerView.inputAccessoryView = pickerToolBar;
    self.pickerView = pickerView;
    [containerView addSubview:pickerView];
}

#pragma mark -- pickerView delegate
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    
    if (0 == component) {
        
        return 10;
    }
    
    return 3;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    
    if (0 == component) {
        return self.pickerArray1[row];
    }
    
    return self.pickerArray2[row];
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    
    return 30;
}

// 设置字体大小
//- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
//    
//    UILabel *pickerLabel = (UILabel *)view;
//    if (!pickerLabel) {
//        pickerLabel = [[UILabel alloc] init];
//        pickerLabel.font = [UIFont boldSystemFontOfSize:15];
//    }
//    
//    pickerLabel.text = [self pickerView:pickerView titleForRow:row forComponent:component];
//    return pickerView;
//}

// pickerView 点击选中
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
    WZLog(@"component = %zd, row = %zd", component, row);
    if (1 == component) {
        
        switch (row) {
                
            case 0:
                self.detailSchedule.alertFlag = @"2";
                self.detailSchedule.beforeUnitName = @"天";
                self.detailSchedule.beforeUnitFlag = @"3";
                
                // 新建日程Vc
                self.addFlag = @"2";
                self.addUnitName = @"天";
                
                break;
                
            case 1:
                self.detailSchedule.alertFlag = @"2"; // 提醒
                self.detailSchedule.beforeUnitName = @"小时";
                self.detailSchedule.beforeUnitFlag = @"2";
                
                // 新建日程Vc
                self.addFlag = @"2";
                self.addUnitName = @"小时";
                
                break;
                
            case 2:
                self.detailSchedule.alertFlag = @"2"; // 提醒
                self.detailSchedule.beforeUnitName = @"分钟";
                self.detailSchedule.beforeUnitFlag = @"1";
                
                // 新建日程Vc
                self.addFlag = @"2";
                self.addUnitName = @"分钟";
                
                break;
                
            default:
                break;
        }
        
    } else if (0 == component) {
        
        self.detailSchedule.beforeLength = [NSString stringWithFormat:@"%@", self.pickerArray1[row]];
    }
    
}


// 按钮点击事件
- (void)cancelBtnDidClick {
    
    [UIView animateWithDuration:0.25 animations:^{
        
        self.containerView.y = self.view.height;
    }];
}

- (void)tooBarDoneBtnDidClick {
    
    [UIView animateWithDuration:0.25 animations:^{
        
        self.containerView.y = self.view.height;
    }];
}

- (void)backTodayBtnDidClick {
    
    
}

// set cell
- (NSString *)remindTime:(NSIndexPath *)indexPath {
    
    NSString *time = nil;
    
    if (0 == indexPath.section) {
        switch (indexPath.row) {
            case 0:
                time = @"不提前提醒";
                break;
                
            case 1:
                
                time = @"提前5分钟";
                break;
                
            case 2:
                
                time= @"提前15分钟";
                break;
            case 3:
                
                time = @"提前30分钟";
                break;
                
            case 4:
                
                time = @"提前1小时";
                break;
                
            default:
                break;
        }
        
    } else {
        
        time = @"自定义";
    }
    
    return time;
}


// tableView Header
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *header = [[UIView alloc] init];
    header.backgroundColor = WZRGBColor(243, 243, 243);
    
    return header;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 20;
}

// 懒加载
- (NSArray *)pickerArray1 {
    
    if (_pickerArray1 == nil) {
        _pickerArray1 = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"10", nil];
    }
    return _pickerArray1;
}

- (NSArray *)pickerArray2 {
    
    if (_pickerArray2 == nil) {
        _pickerArray2 = [NSArray arrayWithObjects:@"天", @"时", @"分", nil];
    }
    return _pickerArray2;
}


@end
